﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AIC_KQKT
{
    public partial class UCDanhMucTaiLieu : System.Web.UI.UserControl
    {
        private static int ID;
        private string MyAction = MyConst.Action.Addnew;
        private static string MyMaDM = "";

        #region Ham Su kien
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermision();
            formUpdate.Visible = false;
            Config();
            if (!IsPostBack)
            {
                BindDataToGrid(MyMaDM, "");
            }
        }

        private void Config()
        {
            AIC_KQKT.Properties.Settings setting = new AIC_KQKT.Properties.Settings();
            radTaiLieuID1_DM.Text = setting.TaiLieuID1_DM;
            radTaiLieuID2_DM.Text = setting.TaiLieuID2_DM;
            radTaiLieuID3_DM.Text = setting.TaiLieuID3_DM;
            radTaiLieuID4_DM.Text = setting.TaiLieuID4_DM;
            radTaiLieuID5_DM.Text = setting.TaiLieuID5_DM;
            radTaiLieuID6_DM.Text = setting.TaiLieuID6_DM;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }


        protected void grdDanhMuc_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            ID = Convert.ToInt32(e.CommandArgument);


            if (e.CommandName == MyConst.Action.Update)
            {
                formUpdate.Visible = true;
                DataTable DanhMucTable = GetChiTietDanhMuc(ID);
                txtMaDanhMuc.Text = DanhMucTable.Rows[0][1].ToString();
                txtTenDanhMuc.Text = DanhMucTable.Rows[0][2].ToString();
                btnLuu.CommandName = MyConst.Action.Update;
            }
            else if (e.CommandName == MyConst.Action.Delete)
            {
                string sql = "DELETE FROM dbo.tblDanhMuc WHERE ID = @ID";
                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                BindDataToGrid(MyMaDM, "");
            }
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            formUpdate.Visible = true;
            txtMaDanhMuc.Text = MyMaDM;
            txtTenDanhMuc.Text = "";
            btnLuu.CommandName = MyConst.Action.Addnew;
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = Convert.ToInt32(Session["userid"]);
                if (btnLuu.CommandName == MyConst.Action.Addnew)
                {
                    InsertDanhMuc(MyMaDM, txtTenDanhMuc.Text, UserID);
                    BindDataToGrid(MyMaDM, "");
                    MyCommon.AddMessage(this.Page.Master, "Thêm mới thành công !", MyConst.MessageType.Success);
                }
                else if (btnLuu.CommandName == MyConst.Action.Update)
                {
                    UpdateDanhMuc(ID, txtTenDanhMuc.Text, UserID);
                    BindDataToGrid(MyMaDM, "");
                    MyCommon.AddMessage(this.Page.Master, "Cập nhật thành công !", MyConst.MessageType.Success);
                }
            }
            catch (Exception)
            {
                MyCommon.AddMessage(this.Page.Master, "Lỗi hệ thống !", MyConst.MessageType.Error);
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            formUpdate.Visible = false;
        }
        #endregion Ham su kien

        #region Ham chuc nang

        protected void BindDataToGrid(string MaDanhMuc, string TenDanhMuc)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@MaDM", SqlDbType.NVarChar, ParameterDirection.Input, MaDanhMuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@TenDM", SqlDbType.NVarChar, ParameterDirection.Input, TenDanhMuc));
            string sql = "SELECT DM.ID ,DM.MaDM ,DM.TenDM , " +
                         "CONVERT(NVARCHAR(10),DM.CreateDate,103)+' '+ CONVERT(NVARCHAR(10),DM.CreateDate,108) CreateDate, " +
                         "CONVERT(NVARCHAR(10),DM.UpdateDate ,103)+' '+ CONVERT(NVARCHAR(10),DM.UpdateDate ,108) UpdateDate, " +
                         "U.HoTen " +
                         "FROM dbo.tblDanhMuc DM " +
                         "LEFT JOIN dbo.tblUser U ON U.id = DM.UserID " +
                         "WHERE DM.MaDM = @MaDM AND (DM.TenDM like @TenDM OR @TenDM = '')";
            grdDanhMuc.DataSource = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            grdDanhMuc.DataBind();
        }

        protected DataTable GetChiTietDanhMuc(int ID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, ID));
            return SqlHelper.ExecuteDataTable(CommandType.Text, "Select * from tblDanhMuc where ID = @ID", arrParams);
        }

        protected void InsertDanhMuc(string MaDanhMuc, string TenDanhMuc, int UserID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@MaDanhMuc", SqlDbType.NVarChar, ParameterDirection.Input, MaDanhMuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@TenDanhMuc", SqlDbType.NVarChar, ParameterDirection.Input, TenDanhMuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, UserID));
            string sql = "INSERT  INTO dbo.tblDanhMuc(" +
                         "MaDM,TenDM,CreateDate,UpdateDate,UserID)" +
                         "VALUES  (@MaDanhMuc,@TenDanhMuc,GETDATE(),GETDATE(),@UserID)";
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        protected void UpdateDanhMuc(int IDDanhMuc, string TenDanhMuc, int UserID)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@IDDanhMuc", SqlDbType.Int, ParameterDirection.Input, IDDanhMuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@TenDanhMuc", SqlDbType.NVarChar, ParameterDirection.Input, TenDanhMuc));
            arrParams.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, UserID));
            string sql = "UPDATE dbo.tblDanhMuc SET TenDM=@TenDanhMuc, UpdateDate=GETDATE(),UserID=@UserID WHERE ID = @IDDanhMuc";
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        public void CheckPermision()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("DanhMucTaiLieu");
            if (Role == null || Role["Xem"] == 0)
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }
        #endregion  Ham chuc nang

        protected void radTaiLieuID_DM_CheckedChanged(object sender, EventArgs e)
        {
            formUpdate.Visible = false;
            int id = 0;
            if (radTaiLieuID1_DM.Checked)
            {
                id = 1;
            }
            else if (radTaiLieuID2_DM.Checked)
            {
                id = 2;
            }
            else if (radTaiLieuID3_DM.Checked)
            {
                id = 3;
            }
            else if (radTaiLieuID4_DM.Checked)
            {
                id = 4;
            }
            else if (radTaiLieuID5_DM.Checked)
            {
                id = 5;
            }
            else if (radTaiLieuID6_DM.Checked)
            {
                id = 6;
            }
            switch (id)
            {
                case 1:
                    BindDataToGrid("TaiLieuID1_DM", "");
                    MyMaDM = "TaiLieuID1_DM";
                    break;
                case 2:
                    BindDataToGrid("TaiLieuID2_DM", "");
                    MyMaDM = "TaiLieuID2_DM";
                    break;
                case 3:
                    BindDataToGrid("TaiLieuID3_DM", "");
                    MyMaDM = "TaiLieuID3_DM";
                    break;
                case 4:
                    BindDataToGrid("TaiLieuID4_DM", "");
                    MyMaDM = "TaiLieuID4_DM";
                    break;
                case 5:
                    BindDataToGrid("TaiLieuID5_DM", "");
                    MyMaDM = "TaiLieuID5_DM";
                    break;
                case 6:
                    BindDataToGrid("TaiLieuID6_DM", "");
                    MyMaDM = "TaiLieuID6_DM";
                    break;
                default:
                    break;
            }
        }
    }
}